<template>
	<view class="video-box fboxWrap Ycenter Xcenter">
		<template v-if="src && progress == '100%'" style="position: relative;z-index: 0;">
			<video v-if="!isDestroy" class="video" id="myVideo" ref="myVideo" loop autoplay :src="src" :controls="false"
				:poster="poster+'?time='" :show-fullscreen-btn="false"
				@play="videoPlay" @pause="videoPause"></video>
		</template>
		<view class="video-loading fboxRow Ycenter Xcenter" v-else>
			<view class="font-white" v-if="msg == '对方正在制作中'">{{msg}}</view>
			<template v-else>
				<u-loading-icon :text="progress ? msg : '正在制作中'" textSize="16" color="#fff" textColor="#fff">
				</u-loading-icon>
			</template>
		</view>
		<image class="video-veil" @click="videoTap" src="https://ebc-prod.cdn.benwunet.com/uni-static/activity-year/video-veil.png"></image>
	</view>
</template>
<script>
	export default {
		props: {
			src: {
				type: String,
				default: ''
			},
			poster: {
				type: String,
				default: ''
			},
			progress: {
				type: String,
				default: '100%'
			},
			msg: {
				type: String,
				default: '正在合成'
			},
			isPlay: {
				type: Boolean,
				default: false
			},
			isDestroy: {
				type: Boolean,
				default: false
			},
			
		},
		data() {
			return {
				pause: false,
				videoContext: null,
				webviewStyles: {
					progress: {
						color: '#FF3333'
					}
				},
				tempVideo: '',
				temopTime: new Date().getTime(),
				isVideo: true
			}
		},
		watch: {
			src: {
				handler(val) {
					console.log('handler', val)
					this.getVideo()
				}
			},
			isPlay: {
				handler(val) {
					console.log('isPlay', val)
					if (val) {
						this.videoPause()
					// } else {
					// 	if (this.pause) {
					// 		this.videoPlay()
					// 	}
					}
				},
				deep:true,
				immediate: true
			},
			isDestroy: {
				handler(val) {
					console.log('销毁：isDestroy',val)
				},
				deep:true,
				immediate: true
			},
		},
		mounted() {
			const videoContext = uni.createVideoContext('myVideo', this);
			if (videoContext) {
				this.videoContext = videoContext
			} else {
				this.$nextTick(() => {
					this.videoContext = uni.createVideoContext('myVideo', this);
				})
			}
			console.log('mounted')
			// this.getVideo()
		},
		methods: {
			eventPause(){
				// var video = document.getElementById('myVideo')
				const video = this.$refs.myVideo
				// 9、pause：暂停监听
				video.addEventListener('pause', function(e) {
				   alert('暂停播放')
				  console.log(e)
				})
			},
			getVideo() {
				this.tempVideo = this.src
				console.log(this.src)
				// uni.downloadFile({
				// 	url: this.src,
				// 	success: (tempRes) => {
				// 		console.log(tempRes);
				// 		this.tempVideo = tempRes.tempFilePath
				// 	},
				// 	fail: (err) => {
				// 		this.tempVideo = this.src
				// 	}
				// })
			},
			loadVideo(e) {
				console.log('loadVideo')
				console.log(e)
				if (this.videoContext) {
					this.videoContext.play(); // 播放
					if (e) {
						this.pause = false
					}
				}
			},
			videoPlay(e) {
				console.log(e)
				// alert('videoPlay')
				if (this.videoContext) {
					this.videoContext.play(); // 播放
					if (e) {
						this.pause = true
					}
				}
			},
			videoPause(e) {
				console.log(e)
				// alert('videoPause')
				if (this.videoContext) {
					this.videoContext.pause(); // 播放
					if (e) {
						this.pause = false
					}
				}
			},
			videoChange(){
				alert('videoChange')
			},
			videoTap() {
				// let vuePlayer = null
				// const player = document.getElementById('myVideo')
				//  if (!vuePlayer) vuePlayer = player.getVueInstance(); // 获取播放器实例
				//   var attrs = player.attributes;
				//   console.log('player', vuePlayer)
				//   vuePlayer.pause = true
				this.videoContext = uni.createVideoContext('myVideo', this);
				if (this.pause) {
					this.videoContext.pause(); // 暂停
					this.pause = false
				} else {
					this.videoContext.play(); // 播放
					this.pause = true
				}
			}
		}
	}
</script>

<style scoped>
	.video-box {
		position: relative;
		width: 750rpx;
		height: 664rpx;
		background-color: #6d0d12;
		z-index: 0;
	}

	.video-box .video {
		position: relative;
		width: 750rpx;
		height: 500rpx;
		z-index: 0;
	}

	.video-box .video-veil {
		position: absolute;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 2;
		width: 750rpx;
		height: 664rpx;
	}

	.video-loading {
		position: relative;
		width: 750rpx;
		height: 664rpx;
		background-color: rgba(0, 0, 0, .5);
	}
</style>
